package org.hospital.action;

import java.util.List;

import org.hospital.dao.IUserDAO;
import org.hospital.pojo.TUser;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LoginAction {

	private TUser user;

	private AbstractApplicationContext aac = new ClassPathXmlApplicationContext(
			"applicationContext.xml");

	public TUser getUser() {
		return user;
	}

	public void setUser(TUser user) {
		this.user = user;
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String execute() throws Exception {

		IUserDAO userDao = (IUserDAO) aac.getBean("UserDAO");
		List userList = null;
		try {
			userList = userDao.findByUsername(user.getUsername());

		} catch (Exception e) {
			System.err.println(e);
		}
		if (userList != null) {
			TUser tempUser = (TUser) userList.get(0);
			if (tempUser != null
					&& tempUser.getPassword().equals(user.getPassword())) {
				return "success";
			} else {
				return "error";
			}
		} else {
			return "error";
		}

	}
}
